<?php

namespace IDock\Insurance\AMIC\Request;

use IDock\Http\RequestFormat;
use IDock\Insurance\AMIC\Request\AllAMICRequest;
use IDock\Insurance\AMIC\Response\PolicyPaymentResponse;
use IDock\Printer\NonCar\NonCarInquirySinglePrinter;
use IDock\Visitor\PolicyPaymentVisitor;
use stdClass;

class PolicyPaymentRequest extends AllAMICRequest
{
    public $requestType = 'N0205';

    protected $response = PolicyPaymentResponse::class;

    protected $urlTip = '支付接口';
    /**
     * @var  PolicyPaymentVisitor $visitor
     */
    protected $visitor;

    public function generate(): array
    {
        $policyPrmList = [];

        $nonCarFee = $this->visitor->argc['nonCarFee'] ?? 0;

        if(!empty($this->visitor->biInquiryNo)){

            $policyPrmList[] = [
                'proposalNo' => $this->visitor->biInquiryNo,
                'expectPrm' => floatval($this->visitor->argc['biPremium'] ?? 0) + $nonCarFee,
            ];
            $nonCarFee = 0;
        }

        if(!empty($this->visitor->ciInquiryNo)){

            $policyPrmList[] = [
                'proposalNo' => $this->visitor->ciInquiryNo,
                'expectPrm' => floatval($this->visitor->argc['ciPremium'] ?? '') + floatval($this->visitor->argc['carShipTax'] ?? '') + $nonCarFee,
            ];
        }
        $sum = 0;
        foreach ($policyPrmList as $value){
            $sum += $value['expectPrm'];
        }
        $data = [
            'payBase' => [
                'orderNo' => $this->visitor->argc['orderNo'] ?? '',
                'sumPrm' => round($sum,2),
                'notifyURL' => $this->visitor->argc['notifyURL'] ?? '',
                'policyPrmList' => ['policyPrm' => $policyPrmList]
            ],

        ];

        return $data;
    }

    /**
     * @param array $data
     * @return string
     */
    private function sign(array $data): string
    {
        $data['KEY'] = $this->payKey;
        $data = urldecode(http_build_query($data));

        $string = urldecode($data);

        $string = strtoupper(md5($string));

        return $string;
    }

    /**
     * 订单列表
     * @return array
     */
    private function goodsList(): array
    {
        $list = [];

        if ($this->visitor->ciPolicyNo) {
            $list[] = [
                'GOODID' => $this->visitor->ciPolicyNo,
                'GOODSOURCE' => '2',
                'GOODDIRECTION' => $this->goodDirection,
                'PAYFEE' => (string)(($this->visitor->argc['ciPremium'] ?? '0') * 100),
            ];
        }

        if ($this->visitor->biPolicyNo) {
            $list[] = [
                'GOODID' => $this->visitor->biPolicyNo,
                'GOODSOURCE' => '2',
                'GOODDIRECTION' => $this->goodDirection,
                'PAYFEE' => (string)(($this->visitor->argc['biPremium'] ?? '0') * 100),
            ];
        }
        //驾意险
        if(!empty($this->visitor->nonCarInquirySingle)) foreach ($this->visitor->nonCarInquirySingle as $val){
            /**@var NonCarInquirySinglePrinter $val */
            $list[] = [
                'GOODID' => $val->inquiryNo,
                'GOODSOURCE' => '1',
                'GOODDIRECTION' => $this->goodDirection,
                'PAYFEE' => (string)round(($val->premium ?? '0') * $val->insuredNum * 100),
            ];
        }
        return $list;
    }

}
